package com.solver.service.impl;

import com.solver.context.BaseContext;
import com.solver.dto.ShoppingCartDTO;
import com.solver.entity.Field;
import com.solver.entity.Question;
import com.solver.entity.ShoppingCart;
import com.solver.mapper.FieldMapper;
import com.solver.mapper.QuestionMapper;
import com.solver.mapper.ShoppingCartMapper;
import com.solver.service.ShoppingCartService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private FieldMapper fieldMapper;


    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        //BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setQuestionId(shoppingCartDTO.getDishId());
        shoppingCart.setQuestionDifficulty(shoppingCartDTO.getDishFlavor());
        shoppingCart.setFieldId(shoppingCartDTO.getSetmealId());
        //只能查询自己的购物车数据
        //解析token时 已经通过threadlocal绑定了userID
        Long id = BaseContext.getCurrentId();
        shoppingCart.setUserId(id);

        //判断当前商品是否在购物车中
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        log.info("shoppingCartList:{}",shoppingCartList.toString());
        log.info("shoppingCartSize:{}",shoppingCartList.size());
        if(shoppingCartList != null && shoppingCartList.size() ==1){
            //商品已经存在 数量加一
            shoppingCart = shoppingCartList.get(0);//只可能存在一条数据
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.updateNumberById(shoppingCart);
        }else {
            //如果不存在 则插入数据
            //判断当前添加的是问题还是套餐
            Long questionId = shoppingCart.getQuestionId();
            if(questionId != null){
                //说明是单个问题
                Question question = questionMapper.getById(questionId);//根据id取出问题
                shoppingCart.setName(question.getName());
                shoppingCart.setImage(question.getImage());
                shoppingCart.setAmount(question.getPrice());
            }else{
                //说明是套餐
                Field field = fieldMapper.getById(shoppingCart.getFieldId());
                shoppingCart.setName(field.getName());
                shoppingCart.setImage(field.getImage());
                shoppingCart.setAmount(field.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> listShoppingCart() {
        Long id= BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(id);
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void cleanShoppingCart() {
        Long id = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(id);
        shoppingCartMapper.cleanShoppingCart(shoppingCart);
    }

    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        Long id = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(id);
        shoppingCart.setQuestionId(shoppingCartDTO.getDishId());
        shoppingCart.setQuestionDifficulty(shoppingCartDTO.getDishFlavor());
        shoppingCart.setFieldId(shoppingCartDTO.getSetmealId());
        shoppingCartMapper.sub(shoppingCart);
    }
}
